package com.pioneer.util;

import java.sql.SQLException;
import java.sql.Connection;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DBUtil
{

	public static Connection getDBConnection() throws DBException
	{
         
		Connection connection=null;
         
		try
		{
			InitialContext ic = new InitialContext();
			DataSource ds = (DataSource)ic.lookup(JNDINames.DATASOURCE);			
			connection = ds.getConnection();
			return connection;        
        }
		catch (NamingException ne)
		{
			throw new DBException("Couldn't get the JNDI name because of "+ne.toString());
		}
		catch (SQLException se)
		{
	    	throw new DBException("Couldn't make the connection because of "+se.toString());
		}
	}
	
	public static String nullOrQuoted(String pString)
	{
		if (pString == null) {
			return "null";
		} else {
			return "'" + SqlHelper.sqlSafe(pString) + "'";
		}
	}
	
	
	public static String nullOrQuoted(java.util.Date pDate)
	{
		if (pDate == null) {
			return "null";
		} else {
			return "'" + new java.sql.Date(pDate.getTime()) + "'";
		}

	}
	

	public static String nullifyEmpty(String pString)
	{
		if (pString == null) {
			return null;
		}
		
		String trimmedString = pString.trim();
		if (trimmedString.length() == 0) {
			return null;
		} else {
			return trimmedString;
		}
	}

	public static String emptyNull(String pString)
	{
		if (pString == null) {
			return "";
		} else {
			return pString;
		}
	}	
}	